package com.hrg.rds.infra.executor.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.hrg.rds.biz.waybill.domain.instance.WaybillInstanceDto;
import com.hrg.rds.biz.waybill.domain.instance.WaybillInstanceVo;
import com.hrg.rds.biz.waybill.domain.template.cmd.CmdParam;
import com.hrg.rds.biz.waybill.service.WaybillInstanceService;
import com.hrg.rds.infra.async.executor.Async;
import com.hrg.rds.infra.executor.CmdExecutor;
import com.hrg.rds.infra.executor.CmdInstanceContext;
import com.hrg.rds.infra.executor.CmdInstanceResult;
import com.hrg.rds.infra.executor.ExecutorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("JoinCmd")
public class JoinCmdExecutor implements CmdExecutor {

	@Override
	public CmdInstanceResult execute(List<CmdParam> params, CmdInstanceContext cmdInstanceContext) {
		log.info("JoinCmdExecutor execute");
		// 遍历执行到那一步骤了，运单最新状态为执行中（暂停后退出流程、继续时重新查询生成流程）
		// 构造下一个执行器
		WaybillInstanceDto waybillInstanceDto = new WaybillInstanceDto();
		waybillInstanceDto.setId(cmdInstanceContext.getWaybillInstanceId());
		try {
			WaybillInstanceVo waybillInstanceVo = SpringUtil.getBean(WaybillInstanceService.class).selectById(waybillInstanceDto);
			Async.beginWorkAsync(ExecutorUtil.wrapWorker(waybillInstanceVo));
		} catch (Exception e) {
			//cmdInstanceContext.setException();
			log.error("JoinCmdExecutor execute error", e);
			return CmdInstanceResult.exception(e.getMessage());
		}
		return CmdInstanceResult.success();
	}
}
